package com.mhc.suzuki.service.impl;

import com.mhc.fiat.api.message.ProcessMessage;
import com.mhc.suzuki.bo.MessageBO;
import com.mhc.suzuki.bo.MessageQueryBO;
import com.mhc.suzuki.bo.SubmitTaskBO;
import com.mhc.suzuki.constant.*;
import com.mhc.suzuki.dal.mapper.ext.*;
import com.mhc.suzuki.dal.model.*;
import com.mhc.suzuki.dal.query.*;
import com.mhc.suzuki.exception.SuzukiCommonException;
import com.mhc.suzuki.service.MessageService;
import com.mhc.suzuki.util.BeanCopierUtil;
import com.mhc.suzuki.websocket.MessageWebSocket;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * Created by HW
 * on 2017/9/8 14:14.
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class MessageServiceImpl implements MessageService {


    @Autowired
    private MessagesExtMapper messagesExtMapper;

    @Autowired
    private OrderExtMapper orderExtMapper;

    @Autowired
    private CarInfoExtMapper carInfoExtMapper;

    @Autowired
    private LoanInfoExtMapper loanInfoExtMapper;

    @Autowired
    private CreditQueryApplyExtMapper creditQueryApplyExtMapper;


    @Autowired
    private OrderCustomerRelExtMapper orderCustomerRelExtMapper;


    @Autowired
    private PaymentRequestExtMapper paymentRequestExtMapper;

    @Autowired
    private ModifyHisExtMapper modifyHisExtMapper;

    //消息内容string
    private static final String T_ONE = "您有新的订单";
    private static final String T_TWO = "（订单号";
    private static final String T_THREE = "， 客户";
    private static final String T_FOUR = "， 购买车型";
    private static final String T_FIVE = "， 贷款额";
    private static final String T_SIX = "元， 业务员";
    private static final String T_SEV = "） 需要审核。";
    private static final String T_EIT = "） 需要您的审核。";
    private static final String T_NINE = "有一笔";
    private static final String T_TEN = "） 需要通融审核。";
    private static final String T_ELE = "） 需要审核确认。";
    private static final String T_TEL = "有一笔订单";
    private static final String T_THR = "） 需要过件审核。";

    /**
     * 审核申请过程中对应发送消息方法
     * @param processMessage 用于获取订单信息， 操作
     * @param applyType 申请类型
     * @throws SuzukiCommonException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sendMessage(ProcessMessage processMessage, Integer applyType) throws SuzukiCommonException,IOException {
        MessagesDO messagesDO = createMessage(processMessage, applyType);//消息生成
        int i =messagesExtMapper.insertSelective(messagesDO);//消息插入
        if(i != 1){//插入错误
            throw new SuzukiCommonException(ErrorCodeEnum.INSERT_OR_UPDATA_ERROR);
        }
        MessageBO messageBO = BeanCopierUtil.convert(messagesDO, MessageBO.class);
        MessageWebSocket.sendMessage(messageBO);

    }


    /**
     * 审核流程结束消息发送
     * @param processMessage 用于获取订单信息， 操作
     * @param applyType 申请类型
     * @throws SuzukiCommonException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sendEndMessage(ProcessMessage processMessage, Integer applyType) throws SuzukiCommonException, IOException {
        //获取订单信息
        String queryOrderNo = processMessage.getBusinessKey();
        OrderQuery orderQuery = new OrderQuery();
        orderQuery.createCriteria().andOrderNoEqualTo(queryOrderNo)
                .andOrderStatusNotEqualTo(OrderStatusEnum.CANCELLED.getCode());
        List<OrderDO> orderDOS = orderExtMapper.selectByQuery(orderQuery);
        if(orderDOS.size() != 1){
            throw new SuzukiCommonException(ErrorCodeEnum.DATA_ERROR);
        }
        OrderDO orderDO = orderDOS.get(0);


        MessagesDO messagesDO = createEndMessage(processMessage, applyType, orderDO);//消息生成
        int i =messagesExtMapper.insertSelective(messagesDO);//消息插入
        if(i != 1){//插入错误
            throw new SuzukiCommonException(ErrorCodeEnum.INSERT_OR_UPDATA_ERROR);
        }
        MessageBO messageBO = BeanCopierUtil.convert(messagesDO, MessageBO.class);
        MessageWebSocket.sendMessage(messageBO);

        //判断是否为贷款审核结束，且订单需要安装GPS 发送gps录入消息
        if(ApplyTypeEnum.LOAN_APPLY.getCode().equals(applyType) || ApplyTypeEnum.ACCOM_APPLY.getCode().equals(applyType)){
            CarInfoQuery carInfoQuery = new CarInfoQuery();
            carInfoQuery.createCriteria().andOrderNoEqualTo(queryOrderNo);
            List<CarInfoDO> carInfoDOS = carInfoExtMapper.selectByQuery(carInfoQuery);
            if(CollectionUtils.isEmpty(carInfoDOS)){
                throw new SuzukiCommonException(ErrorCodeEnum.CARINFO_NOT_EXIST);
            }
            CarInfoDO carInfoDO = carInfoDOS.get(0);
            if (carInfoDO.getNeedGps()){
                log.info("该订单需要安装GPS");
                MessagesDO bo = new MessagesDO();
                bo.setStaffId(orderDO.getStaffId());
                bo.setMessageTitle("GPS录入提示");
                bo.setBizType(orderDO.getBizChannel());
                bo.setPriority(MessagePriorityEnum.LOW.getCode());
                bo.setStatus(MessageStatusEnum.UNREAD.getCode());
                bo.setMessageContent("您的客户" + orderDO.getCustomerName() + "（订单号" +
                        orderDO.getOrderNo() + "），需要安装gps，请前往补录。");
                int j =messagesExtMapper.insertSelective(bo);//消息插入
                if(j != 1){//插入错误
                    throw new SuzukiCommonException(ErrorCodeEnum.INSERT_OR_UPDATA_ERROR);
                }
                MessageBO messageGPSBO = BeanCopierUtil.convert(bo, MessageBO.class);
                MessageWebSocket.sendMessage(messageGPSBO);
            }
        } else if (ApplyTypeEnum.MODIFY_APPLY.getCode().equals(applyType)){//修改申请
            PaymentRequestQuery paymentRequestQuery = new PaymentRequestQuery();
            paymentRequestQuery.createCriteria().andOrderNoEqualTo(queryOrderNo)
                    .andApplyStatusEqualTo(PaymentRequestStatusEnum.PAID.getCode());

            List<PaymentRequestDO> paymentRequestDOS = paymentRequestExtMapper.selectByQuery(paymentRequestQuery);
            if (!CollectionUtils.isEmpty(paymentRequestDOS)){//该订单财务已垫款
                ModifyHisDO modifyHisDO = modifyHisExtMapper.getLoanAmountChangeHis(queryOrderNo);
                //财务垫款后的贷款额度修改审核通过
                if(modifyHisDO != null){
                    String oldValue = modifyHisDO.getOldValue();//修改前额度
                    String newValue = modifyHisDO.getNewValue();//修改后额度
                    MessageBO messageLoanBO = new MessageBO();
                    messageLoanBO.setMessageTitle("贷款额度修改消息");
                    messageLoanBO.setPriority(MessagePriorityEnum.HIGH.getCode());
                    messageLoanBO.setBizType(applyType);
                    messageLoanBO.setStaffId(paymentRequestDOS.get(0).getHandleStaffId());//消息发送对应财务ID号
                    messageLoanBO.setStatus(MessageStatusEnum.UNREAD.getCode());
                    //设置消息主体内容

                    String carModel;//品牌车型

                    //查询车辆信息
                    CarInfoQuery carInfoQuery = new CarInfoQuery();
                    carInfoQuery.createCriteria().andOrderNoEqualTo(queryOrderNo);
                    List<CarInfoDO> carInfoDOS = carInfoExtMapper.selectByQuery(carInfoQuery);
                    if (CollectionUtils.isEmpty(carInfoDOS)){
                        throw new SuzukiCommonException(ErrorCodeEnum.CARINFO_NOT_EXIST);//数据库中车辆信息有误
                    } else {
                        carModel = carInfoDOS.get(0).getCarModel();
                    }

                    StringBuilder content = new StringBuilder();
                    content.append("您好，订单号").append(queryOrderNo).append("，客户").append(orderDO.getCustomerName())
                            .append("，购买车型").append(carModel).append("，经销商").append(orderDO.getDealerName())
                            .append("，业务员").append(orderDO.getStaffName()).append("，原垫款额度").append(oldValue)
                            .append("元，已被调整为" ).append(newValue).append("元，请知悉，并做相关处理。");
                    messageLoanBO.setMessageContent(content.toString());

                    MessagesDO messageLoanDO = BeanCopierUtil.convert(messageLoanBO, MessagesDO.class);
                    messagesExtMapper.insertSelective(messageLoanDO);
                    MessageWebSocket.sendMessage(messageLoanBO);
                }
            }

        }

    }


    /**
     * 订单审核不通过时发送的消息
     * @param orderDO
     * @throws SuzukiCommonException
     */
    @Override
    public void sendNoPassMessage(OrderDO orderDO, SubmitTaskBO submitTaskBO) throws SuzukiCommonException, IOException {


        //消息生成
        MessagesDO messageDO = new MessagesDO();
        String title = ApplyTypeEnum.getApplyTypeEnum(submitTaskBO.getApplyType()).getName() + "结束";
        messageDO.setMessageTitle(title);

        StringBuilder context = new StringBuilder();
        context.append("您的客户").append(orderDO.getCustomerName()).append("（订单号").append(orderDO.getOrderNo()).append("） ，")
                .append(ApplyTypeEnum.getApplyTypeEnum(submitTaskBO.getApplyType()).getName()).append("不通过，" + "审核意见为：").append(submitTaskBO.getApprovalRemark());
        messageDO.setMessageContent(context.toString());
        messageDO.setStatus(MessageStatusEnum.UNREAD.getCode());
        messageDO.setPriority(MessagePriorityEnum.HIGH.getCode());
        messageDO.setStaffId(orderDO.getStaffId());
        messageDO.setBizType(submitTaskBO.getApplyType());

        //消息保存及发送
        int i =messagesExtMapper.insertSelective(messageDO);//消息插入
        if(i != 1){//插入错误
            throw new SuzukiCommonException(ErrorCodeEnum.INSERT_OR_UPDATA_ERROR);
        }
        MessageBO messageBO = BeanCopierUtil.convert(messageDO, MessageBO.class);
        MessageWebSocket.sendMessage(messageBO);
    }

    /**
     * 征信录入消息发送
     * @param orderNo
     */
    @Override
    @Transactional
    public void sendCreditMessage(String orderNo) throws SuzukiCommonException,IOException{

        //根据订单编号查询订单信息
        OrderQuery orderQuery = new OrderQuery();
        orderQuery.createCriteria().andOrderNoEqualTo(orderNo)
                    .andOrderStatusNotEqualTo(OrderStatusEnum.CANCELLED.getCode());
        List<OrderDO> orderDOS = orderExtMapper.selectByQuery(orderQuery);
        if (orderDOS.size() != 1){
            throw new SuzukiCommonException(ErrorCodeEnum.DATA_ERROR);
        }
        OrderDO orderDO = orderDOS.get(0);

        //获取用户信息,及征信信息
        Long customerId = orderDOS.get(0).getCustomerId();

        //获取征信编号
        OrderCustomerRelQuery orderCustomerRelQuery = new OrderCustomerRelQuery();
        orderCustomerRelQuery.createCriteria().andCustomerIdEqualTo(customerId)
                                            .andIsDeletedNotEqualTo(IsDeletedEnum.DELETED.getCode());
        List<OrderCustomerRelDO> orderCustomerRelDOS = orderCustomerRelExtMapper.selectByQuery(orderCustomerRelQuery);
        if(orderCustomerRelDOS.size() != 1){
            throw new SuzukiCommonException(ErrorCodeEnum.DATA_ERROR);
        }

        //获取征信记录表
        Long creditId = orderCustomerRelDOS.get(0).getCreditApplyId();
        CreditQueryApplyQuery creditQueryApplyQuery = new CreditQueryApplyQuery();
        creditQueryApplyQuery.createCriteria().andCreditQueryApplyIdEqualTo(creditId);
        List<CreditQueryApplyDO> creditQueryApplyDOS = creditQueryApplyExtMapper.selectByQuery(creditQueryApplyQuery);
        if (creditQueryApplyDOS.size() != 1){
            throw new SuzukiCommonException(ErrorCodeEnum.DATA_ERROR);
        }
        CreditQueryApplyDO creditQueryApplyDO = creditQueryApplyDOS.get(0);


        //消息合成
        MessagesDO messageDO = new MessagesDO();
        LocalDateTime localDateTime = creditQueryApplyDO.getGmtModified();
        String time = localDateTime.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日HH时mm分"));
        String context = "您的客户" + orderDO.getCustomerName() +
                    "(订单号" + orderNo + "), " + orderDO.getLoanBankName() +
                    "的征信结果如下： " + creditQueryApplyDO.getCreditResult() +
                    "。 录入人员： " + creditQueryApplyDO.getHandleStaffName() +
                    "， 录入时间： " + time + "。";
        log.info("征信录入消息" + context);
        messageDO.setBizType(ApplyTypeEnum.CREDITED.getCode());
        messageDO.setPriority(MessagePriorityEnum.LOW.getCode());//低优先级
        messageDO.setStatus(MessageStatusEnum.UNREAD.getCode());
        messageDO.setMessageContent(context);
        messageDO.setMessageTitle("征信结果录入通知");
        messageDO.setStaffId(creditQueryApplyDO.getStaffId());//消息对应员工

        int i =messagesExtMapper.insertSelective(messageDO);//消息插入
        if(i != 1){//插入错误
            throw new SuzukiCommonException(ErrorCodeEnum.INSERT_OR_UPDATA_ERROR);
        }
        MessageBO messageBO = BeanCopierUtil.convert(messageDO, MessageBO.class);
        MessageWebSocket.sendMessage(messageBO);
    }


    /**
     * 条件查询消息
     * @param messageQueryBO 前端输入的条件
     * @return
     * @throws SuzukiCommonException
     */
    @Override
    public List<MessageBO> getMessagesByQueryBO(MessageQueryBO messageQueryBO) throws SuzukiCommonException {
        log.info("开始查询消息列表");
        Map<String, Object> queryMap = setQuery(messageQueryBO);
        return messagesExtMapper.queryMessagesByMap(queryMap);
    }

    /**
     * 条件查询消息数
     * @param messageQueryBO
     * @return
     * @throws SuzukiCommonException
     */
    @Override
    public Integer getMessagesCountByQueryBO(MessageQueryBO messageQueryBO) throws SuzukiCommonException {
        log.info("开始查询消息列表");
        Map<String, Object> queryMap = setQuery(messageQueryBO);
        return messagesExtMapper.countMessagesByMap(queryMap);
    }

    @Override
    public List<MessageBO> getMessagesNotDeletedByQueryBO(MessageQueryBO messageQueryBO) throws SuzukiCommonException {
        log.info("开始查询未删除消息列表");
        Map<String, Object> queryMap = setQuery(messageQueryBO);
        return messagesExtMapper.queryMessagesNotDeletedByMap(queryMap);
    }

    @Override
    public Integer getMessagesCountNotDeletedByQueryBO(MessageQueryBO messageQueryBO) throws SuzukiCommonException {
        log.info("开始查询未删除消息列表");
        Map<String, Object> queryMap = setQuery(messageQueryBO);
        return messagesExtMapper.countMessagesNotDeletedByMap(queryMap);
    }

    /**
     * 标记消息为已读
     * @param messagesId
     * @return
     * @throws SuzukiCommonException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean changeStatus(Long messagesId, Integer status) throws SuzukiCommonException {
        if (messagesId == null){
            return false;
        }
        MessagesDO messagesDO = new MessagesDO();
        messagesDO.setId(messagesId);
        messagesDO.setStatus(status);
        if (messagesExtMapper.updateByPrimaryKeySelective(messagesDO) == 1){
            return true;
        } else {
            throw new SuzukiCommonException(ErrorCodeEnum.INSERT_OR_UPDATA_ERROR);
        }
    }


    /**
     * 批量标记消息为已读
     * @param messagesIds
     * @return
     * @throws SuzukiCommonException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean changeStatusByList(Long[] messagesIds) throws SuzukiCommonException {
        if (messagesIds == null){
            return false;
        }

        MessagesQuery messagesQuery = new MessagesQuery();
        for(Long messageId : messagesIds){
            messagesQuery.or().andIdEqualTo(messageId);
        }
        MessagesDO messagesDO = new MessagesDO();
        messagesDO.setStatus(MessageStatusEnum.READ.getCode());
        if(messagesExtMapper.updateByQuerySelective(messagesDO, messagesQuery) == messagesIds.length){
            return true;
        } else {
            throw new SuzukiCommonException(ErrorCodeEnum.INSERT_OR_UPDATA_ERROR);
        }
    }

    /**
     * 设置查询条件
     * @param messageQueryBO
     * @return
     */
    private Map<String, Object> setQuery(MessageQueryBO messageQueryBO){

        Map<String, Object> queryMap = new HashMap<>(64);
        queryMap.put("gmtStart", messageQueryBO.getGmtStart());
        queryMap.put("gmtEnd", messageQueryBO.getGmtEnd());
        queryMap.put("staffId", messageQueryBO.getStaffId());
        queryMap.put("pageSize", messageQueryBO.getPageSize());
        queryMap.put("status", messageQueryBO.getStatus());
        queryMap.put("sort", messageQueryBO.getSort());

        /*计算页偏移数*/
        MessagesQuery messagesQuery = new MessagesQuery();
        messagesQuery.setPageNo(messageQueryBO.getPageNo());
        messagesQuery.setPageSize(messageQueryBO.getPageSize());
        queryMap.put("pageOffset", messagesQuery.getPageOffset());

        return queryMap;
    }




    /**
     * 创建审核流程中消息DO对象
     * @param processMessage
     * @param applyType
     * @return
     */
    private MessagesDO createMessage(ProcessMessage processMessage, Integer applyType) throws SuzukiCommonException{
        MessagesDO messagesDO = new MessagesDO();//发送的消息类
        String title = ApplyTypeEnum.getApplyTypeEnum(applyType).getName() + "通知";//消息标题

        // 根据processMessAge去获取订单信息， 用户信息
        String queryOrderNo = processMessage.getBusinessKey();
        OrderQuery orderQuery = new OrderQuery();
        orderQuery.createCriteria().andOrderNoEqualTo(queryOrderNo)
                            .andOrderStatusNotEqualTo(OrderStatusEnum.CANCELLED.getCode());
        List<OrderDO> orderDOS = orderExtMapper.selectByQuery(orderQuery);
        if(orderDOS.size() != 1){
            throw new SuzukiCommonException(ErrorCodeEnum.DATA_ERROR);
        }
        OrderDO orderDO = orderDOS.get(0);


        String customer = orderDO.getCustomerName();//顾客姓名
        String orderNo = orderDO.getOrderNo();//
        String carModel;//品牌车型

        //查询车辆信息
        CarInfoQuery carInfoQuery = new CarInfoQuery();
        carInfoQuery.createCriteria().andOrderNoEqualTo(queryOrderNo);
        List<CarInfoDO> carInfoDOS = carInfoExtMapper.selectByQuery(carInfoQuery);
        if (CollectionUtils.isEmpty(carInfoDOS)){
            throw new SuzukiCommonException(ErrorCodeEnum.CARINFO_NOT_EXIST);//数据库中车辆信息有误
        } else {
            carModel = carInfoDOS.get(0).getCarModel();
        }

        BigDecimal loanAmount;//贷款金额

        //查询贷款金额
        LoanInfoQuery loanInfoQuery = new LoanInfoQuery();
        loanInfoQuery.createCriteria().andOrderNoEqualTo(orderNo)
                                    .andIsDeletedEqualTo(IsDeletedEnum.NORMAL.getCode());
        List<LoanInfoDO> loanInfoDOS = loanInfoExtMapper.selectByQuery(loanInfoQuery);
        if(loanInfoDOS.size() != 1){
            throw new SuzukiCommonException(ErrorCodeEnum.LOANINFO_TOO_MORE);//数据库中贷款信息有误
        } else {
            loanAmount = loanInfoDOS.get(0).getLoanAmount();
        }

        String mount = loanAmount.setScale(2, BigDecimal.ROUND_HALF_UP).toString();//数值保留两位小数
        Long staffId = Long.parseLong(processMessage.getAssignee()); //下一个节点审核人员ID
        String staffName = orderDO.getStaffName(); //订单创建人

        StringBuilder messageContent = new StringBuilder();
        messagesDO.setMessageTitle(title);//消息标题设置
        messagesDO.setStatus(MessageStatusEnum.UNREAD.getCode());//消息设为未读
        messagesDO.setBizType(applyType);//申请类型
        messagesDO.setStaffId(staffId);//消息对应员工编号

        if(ApplyTypeEnum.LOAN_APPLY.getCode().equals(applyType)){//贷款申请审核消息
            //您有新的订单（订单号***，客户***，购买车型***，贷款额***元，业务员***）需要审核。
            messageContent.append(T_ONE).append(T_TWO).append(orderNo).append(T_THREE).append(customer).append(T_FOUR)
                    .append(carModel).append(T_FIVE).append(mount).append(T_SIX).append(staffName).append(T_SEV);
            log.info("贷款申请:{}", messageContent.toString());
            messagesDO.setMessageContent(messageContent.toString());
            messagesDO.setPriority(MessagePriorityEnum.LOW.getCode());//优先级设置为低，无须手动关闭
        } else if (ApplyTypeEnum.MODIFY_APPLY.getCode().equals(applyType)){//修改审核消息
            //有一笔修改审核（订单号***，客户***，购买车型***，贷款额***元，业务员***）需要您的审核。
            messageContent.append(T_NINE).append(ApplyTypeEnum.getApplyTypeEnum(applyType).getName()).append(T_TWO)
                    .append(orderNo).append(T_THREE).append(customer).append(T_FOUR).append(carModel).append(T_FIVE)
                    .append(mount).append(T_SIX).append(staffName).append(T_EIT);
            log.info("修改申请:{}", messageContent.toString());
            messagesDO.setMessageContent(messageContent.toString());
            messagesDO.setPriority(MessagePriorityEnum.LOW.getCode());//优先级设置为低，无须手动关闭
        } else if (ApplyTypeEnum.ACCOM_APPLY.getCode().equals(applyType)){//通融审核消息
            //您有新的订单（订单号***，客户***，购买车型***，贷款额***元，业务员***）需要通融审核。
            messageContent.append(T_ONE).append(T_TWO).append(orderNo).append(T_THREE).append(customer).append(T_FOUR)
                    .append(carModel).append(T_FIVE).append(mount).append(T_SIX).append(staffName).append(T_TEN);
            log.info("通融申请:{}", messageContent.toString());
            messagesDO.setMessageContent(messageContent.toString());
            messagesDO.setPriority(MessagePriorityEnum.LOW.getCode());//优先级设置为低，无须手动关闭
        } else if (ApplyTypeEnum.PAYMENT_REQUEST_APPLY.getCode().equals(applyType)){//打款申请消息
            //有新的单款申请（订单号***，客户***，购买车型***，贷款额***元，业务员***）需要审核确认。
            messageContent.append("有新的").append(ApplyTypeEnum.getApplyTypeEnum(applyType).getName()).append(T_TWO).append(orderNo).append(T_THREE)
                    .append(customer).append(T_FOUR).append(carModel).append(T_FIVE).append(mount).append(T_SIX).append(staffName).append(T_ELE);
            log.info("打款申请:{}", messageContent.toString());
            messagesDO.setMessageContent(messageContent.toString());
            messagesDO.setPriority(MessagePriorityEnum.HIGH.getCode());//优先级设为高，需要手动关闭消息提醒
        } else if (ApplyTypeEnum.ACCOM_REVIEW_AUDIT.getCode().equals(applyType)){//通融过件审核
            //有一笔订单（订单号***，客户***，购买车型***，贷款额***元，业务员***）需要过件审核。
            messageContent.append(T_TEL).append(customer).append(T_FOUR).append(carModel).append(T_FIVE).append(mount)
                    .append(T_SIX).append(staffName).append(T_THR);
            log.info("通融过件审核:{}", messageContent.toString());
            messagesDO.setMessageContent(messageContent.toString());
            messagesDO.setPriority(MessagePriorityEnum.LOW.getCode());//优先级设置为低，无须手动关闭
        }

        return messagesDO;
    }

    /**
     * 创建流程结束消息DO
     * @param processMessage
     * @param applyType 审核类型
     * @return
     * @throws SuzukiCommonException
     */
    private MessagesDO createEndMessage(ProcessMessage processMessage, Integer applyType, OrderDO orderDO) {
        MessagesDO messagesDO = new MessagesDO();//发送的消息类
        String title = ApplyTypeEnum.getApplyTypeEnum(applyType).getName() + "通知";//消息标题

        String queryOrderNo = processMessage.getBusinessKey();
        //消息发往用户id获取
        Long staffId = orderDO.getStaffId();

        messagesDO.setMessageTitle(title);//消息标题设置
        messagesDO.setStatus(MessageStatusEnum.UNREAD.getCode());//消息设为未读
        messagesDO.setBizType(applyType);//申请类型
        messagesDO.setStaffId(staffId);//消息对应员工编号
        messagesDO.setPriority(MessagePriorityEnum.LOW.getCode());//消息优先级均为低

        String context = "";
        Set<Integer> applySet = new HashSet();
        applySet.add(ApplyTypeEnum.LOAN_APPLY.getCode());
        applySet.add(ApplyTypeEnum.MODIFY_APPLY.getCode());
        applySet.add(ApplyTypeEnum.ACCOM_APPLY.getCode());
        applySet.add(ApplyTypeEnum.PAYMENT_REQUEST_APPLY.getCode());
        applySet.add(ApplyTypeEnum.ACCOM_REVIEW_AUDIT.getCode());

        //贷款，打款，通融，修改，通融申请审核结束消息
        if(applySet.contains(applyType)){
            if(ApplyTypeEnum.PAYMENT_REQUEST_APPLY.getCode().equals(applyType)){//打款申请
                context = "您的客户" + orderDO.getCustomerName() + "(订单号" + queryOrderNo +
                        "）， 财务已垫款";
            } else {
                context = "您的客户" + orderDO.getCustomerName() + "(订单号" + queryOrderNo +
                        "）， " + ApplyTypeEnum.getApplyTypeEnum(applyType).getName() + "通过";
            }
        }
        messagesDO.setMessageContent(context);

        return messagesDO;
    }


}
